var prefix = jsctx + "/ugsMemberSuperLevel";

var ugsMemberSuperLevel = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageUgsMemberSuperLevel",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [10, 20, 30],
                    pageNumber: 1,
                    sidePagination: "server",
                    cache: false,
                    height: h,
                    paginationShowPageGo: true,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            beginTime: $("#beginTime").val().trim(),
                            endTime: $("#endTime").val().trim(),
                            superLevelType: $("#superLevelType").val().trim(),
                            recordType: $("#recordType").val().trim(),
                            mobile: $("#mobile").val().trim(),
                            isValid: $("#isValid").val().trim()
                        };
                    },
                    columns: [
                        {
                            checkbox: true, field: '', width: '50', align: 'left',
                        },
                        {
                            title: '#', field: '',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }, align: 'center'
                        },
                        {field: '', title: '#', visible: false, align: 'center'},
                        {field: 'nickname', title: '昵称', align: 'left'},
                        {field: 'mobile', title: '手机号', align: 'left'},
                        {
                            field: 'superLevelType', title: '类型', align: 'center',
                            formatter: function (value, row, index) {
                                if (value == 'V1') {
                                    return '<span class="label label-danger">月卡</span>'
                                } else if (value == 'V2') {
                                    return '<span class="label label-info">季卡</span>'
                                } else if (value == 'V3') {
                                    return '<span class="label label-primary">年卡</span>'
                                }
                            }
                        },
                        {
                            field: 'recordType', title: '获得途径', align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-danger">购买</span>'
                                } else if (value == '1') {
                                    return '<span class="label label-info">赠送</span>'
                                }
                            }
                        },
                        {field: 'beginTime', title: '有效开始时间', align: 'center'},
                        {field: 'endTime', title: '有效结束时间', align: 'center'},
                        {field: 'totalAmount', title: '累计总金额', align: 'center'},
                        {field: 'userStatus', title: '使用状态', align: 'center'},
                        {
                            title: '操作',
                            field: 'roleId',
                            align: 'center',
                            formatter: function (value, row, index) {
                                // var a = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="编辑" onclick="ugsMemberSuperLevel.forcedFailure(\''
                                //     + row.rlId
                                //     + '\')">强制失效</a> ';
                                // if (row.status == '使用中') {
                                //     return a;
                                // }
                                var b = '';
                                if (MemberManager_supMemberRecord_look) {
                                    b = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="购买记录    " onclick="ugsMemberSuperLevel.buyRecord(\''
                                        + row.memberId
                                        + '\')">购买记录</a> ';
                                }
                                return b;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function editUgsMemberSuperLevel(ulId) {
        location.href = prefix + "/editUgsMemberSuperLevel/" + ulId;
    }

    /*  模态框  */
    function showModel(sort, ulId) {
        //  $("#recipient-name").val(sort)
        //  $("#ulId").val(ulId)
        $('#exampleModal').modal('show')

        $("#giftMember").unbind("click").bind("click", function () {

            var formData = new FormData();
            formData.append("phone", $("#phone").val());
            formData.append("levelType", $("#levelType").val());
            formData.append("days", $("#days").val());
            formData.append("verificationCode", $("#verificationCode").val());
            $.ajax({
                url: prefix + "/saveGiveSuperLevel",
                type: "post",
                data: formData,
                dataType: "json",
                processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        //  $('#exampleModal').modal('hide')
                    }

                }
            })
        })


        // $("#giftMember").click(function () {
        //
        //
        // })
    }


    function updateStatusById(ulId) {
        // 状态 0:未上线  1：启用 2:停止
        layer.confirm('确定更改状态？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updateStatusById",
                type: "post",
                data: {
                    'ulId': ulId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("启用成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })

    };


    // 失效按钮事件
    function batchFailure() {
        var rows = $("#exampleTable").bootstrapTable('getSelections');// 获得要删除的数据
        if (rows.length == 0) {// rows 主要是为了判断是否选中，下面的else内容才是主要
            alert("请先选择要失效的记录!");
            return;
        } else {
            var memberIds = new Array();// 声明一个数组
            $(rows).each(function () {// 通过获得别选中的来进行遍历
                memberIds.push(this.memberId); // cid为获得到的整条数据中的一列
            });
            memberIdList = JSON.stringify(memberIds);
            batchForcedFailure(memberIdList)
        }
    }

    function batchForcedFailure(memberIdList) {
        layer.confirm('确定要强制失效？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/batchForcedFailure",
                type: "post",
                data: {
                    memberIdList: memberIdList
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("失效成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    function forcedFailure(rlId) {
        layer.confirm('确定要强制失效？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/forcedFailure/" + rlId,
                type: "post",
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("失效成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    function addUgsMemberSuperLevel() {
        location.href = prefix + "/addUgsMemberSuperLevel";
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    function empiricalRruleList() {
        location.href = jsctx + "/empiricalRrule/empiricalRrule";
    }

    function userLevelChangeLog(memberId) {
        location.href = prefix + "/userLevelChangeLog/" + memberId;
    }

    /*------  验证码  -----*/
    var num1 = 60;
    var ajaxReady = true;

    function getAuthCode() {
        if (ajaxReady) {
            $.ajax({
                url: prefix + "/getSuperLevelAuthCode",
                type: "POST",
                data: {},
                beforeSend: function () {
                    ajaxReady = false;
                },
                success: function (data) {
                    if (data.status == 200) {
                        time();

                        function time() {
                            if (num1 == 0) {

                                $('#verificationCode').attr("disabled", "disabled")
                                $('#sendVerificationCode').text("获取验证码");
                                $('#sendVerificationCode').attr("onclick", 'ugsMemberSuperLevel.getAuthCode()');
                                ajaxReady = true;
                                num1 = 60;
                            } else {
                                $('#verificationCode').removeAttr('disabled')
                                $('#sendVerificationCode').html(num1 + "秒后重试");
                                $('#sendVerificationCode').removeAttr('onclick');
                                num1--;
                                setTimeout(function () {
                                    time()
                                }, 1000)
                            }
                        }
                    }

                }
            })


        }
    };

    function buyRecord(memberId) {
        location.href = prefix + "/buyRecord/" + memberId;
    }

    return {
        getAuthCode: getAuthCode,
        showModel: showModel,
        forcedFailure: forcedFailure,
        init: init,
        reLoad: reLoad,
        addUgsMemberSuperLevel: addUgsMemberSuperLevel,
        goBack: goBack,
        editUgsMemberSuperLevel: editUgsMemberSuperLevel,
        updateStatusById: updateStatusById,
        empiricalRruleList: empiricalRruleList,
        userLevelChangeLog: userLevelChangeLog,
        batchFailure: batchFailure,
        batchForcedFailure: batchForcedFailure,
        buyRecord: buyRecord
    };
})();

